
"""
    资产表
"""

from django.db import models
from users.models import UserProfileModel


class AssetModel(models.Model):

    """
        所有资产共有数据表
    """

    asset_type_choice = (
        ('server', '服务器'),
        ('network', '网络设备'),
        ('storage', '存储设备'),
        ('security', '安全设备'),
        ('software', '软件资产'),
    )

    asset_status_choice = (
        (0, '在线'),
        (1, '下线'),
        (2, '未知'),
        (3, '故障'),
        (4, '送修'),
        (5, '备用'),
    )

    asset_type = models.CharField(max_length=128, verbose_name=u'资产类型', choices=asset_type_choice, default='server')
    name = models.CharField(max_length=128, verbose_name=u'资产名称', unique=True)
    sn = models.CharField(max_length=128, verbose_name=u'资产序列号', unique=True)
    business_unit = models.ForeignKey('BusinessUnitModel', verbose_name=u'业务线', null=True, blank=True,
                                      on_delete=models.SET_NULL)
    status = models.SmallIntegerField(choices=asset_status_choice, default=0, verbose_name='设备状态')
    manufacturer = models.ForeignKey('ManufacturerModel', verbose_name=u'制造商', null=True, blank=True,
                                     on_delete=models.SET_NULL)
    manage_ip = models.GenericIPAddressField(null=True, blank=True, verbose_name=u'管理IP')
    tags = models.ManyToManyField('TagModel', blank=True, verbose_name='标签')
    admin = models.ForeignKey(UserProfileModel, verbose_name=u'资产管理员', null=True, blank=True, related_name='admin',
                              on_delete=models.SET_NULL)
    idc = models.ForeignKey('IDCModel', null=True, blank=True, verbose_name='所在机房', on_delete=models.SET_NULL)
    contract = models.ForeignKey('ContractModel', null=True, blank=True, verbose_name='合同', on_delete=models.SET_NULL)
    purchase_day = models.DateField(null=True, blank=True, verbose_name="购买日期")
    expire_day = models.DateField(null=True, blank=True, verbose_name="过保日期")
    price = models.FloatField(null=True, blank=True, verbose_name="价格")
    approved_by = models.ForeignKey(UserProfileModel, null=True, blank=True, verbose_name='批准人',
                                    related_name='approved_by', on_delete=models.SET_NULL)
    memo = models.TextField(null=True, blank=True, verbose_name='备注')
    c_time = models.DateTimeField(auto_now_add=True, verbose_name='批准日期')
    m_time = models.DateTimeField(auto_now=True, verbose_name='更新日期')

    def __str__(self):
        return self.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-c_time']
        verbose_name = u'资产总表'
        verbose_name_plural = verbose_name


class IDCModel(models.Model):

    """
        机房
    """

    name = models.CharField(max_length=64, unique=True, verbose_name="机房名称")
    memo = models.CharField(max_length=128, blank=True, null=True, verbose_name='备注')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = '机房'
        verbose_name_plural = "机房"


class ManufacturerModel(models.Model):

    """
        厂商
    """

    name = models.CharField(max_length=64, verbose_name="厂商名称", unique=True)
    telephone = models.CharField(max_length=32, blank=True, null=True, verbose_name="支持电话")
    memo = models.CharField(max_length=128, blank=True, null=True, verbose_name="备注")
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = '厂商'
        verbose_name_plural = "厂商"


class BusinessUnitModel(models.Model):

    """
        业务线
    """

    parent_unit = models.ForeignKey('self', blank=True, null=True, related_name='parent_level',
                                    on_delete=models.CASCADE)
    name = models.CharField(max_length=64, verbose_name=u'业务线名称', unique=True)
    memo = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'备注')
    time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-time']
        verbose_name = '业务线'
        verbose_name_plural = "业务线"


class ContractModel(models.Model):

    """
        合同
    """

    sn = models.CharField(max_length=128, unique=True, verbose_name=u'合同号')
    name = models.CharField(max_length=64, verbose_name=u'合同名称')
    memo = models.TextField(blank=True, null=True, verbose_name=u'备注')
    price = models.IntegerField(verbose_name=u'合同金额')
    detail = models.TextField(blank=True, null=True, verbose_name=u'合同详细')
    start_day = models.DateField(blank=True, null=True, verbose_name=u'开始日期')
    end_day = models.DateField(blank=True, null=True, verbose_name=u'失效日期')
    license_num = models.IntegerField(blank=True, null=True, verbose_name=u'license数量')
    c_day = models.DateField(auto_now_add=True, verbose_name=u'创建日期')
    m_day = models.DateField(auto_now=True, verbose_name=u'修改日期')

    def __str__(self):
        return self.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-c_day']
        verbose_name = '合同'
        verbose_name_plural = "合同"


class TagModel(models.Model):

    """
        标签
    """

    name = models.CharField(max_length=32, unique=True, verbose_name=u'标签名')
    c_day = models.DateField(auto_now_add=True, verbose_name=u'创建日期')

    def __str__(self):
        return self.name

    class Meta:
        app_label = 'cmdb'
        ordering = ['-c_day']
        verbose_name = '标签'
        verbose_name_plural = "标签"
